﻿#region License
/**	
The MIT License (MIT)

Copyright (c) 2013 Philipp Schillinger, Michael Alekseew, Andreas Braun, Steeven Zeiß

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 
 **/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IndoorLocalization {

	/// <summary>
	/// Implementation of the concept of homogenous coordinates.
	/// This class provides functionality for conversion and calculation.
	/// </summary>
	[Serializable]
	public class HCoord {

		private float x;
		/// <summary>
		/// x value of this point.
		/// Has no meaning if this <c>HCoord</c> describes a line.
		/// </summary>
		public int X {	get { return (int)x; }
						set { x = value; } }

		private float y;
		/// <summary>
		/// y value of this point.
		/// Has no meaning if this <c>HCoord</c> describes a line.
		/// </summary>
		public int Y {	get { return (int)y; }
						set { y = value; } }

		/// <summary>
		/// Provides a simple Constructor for Serialization.
		/// </summary>
		private HCoord() { }
		
		/// <summary>
		/// Creates a new point at the given position.
		/// </summary>
		/// <param name="x">x value of the point.</param>
		/// <param name="y">y value of the point.</param>
		public HCoord(float x, float y) {
			this.x = x;
			this.y = y;
		}

		/// <summary>
		/// Creates a new line through the given point in the given direction.
		/// </summary>
		/// <param name="x">x value of the point.</param>
		/// <param name="y">y value of the point.</param>
		/// <param name="direction">Direction of the line.</param>
		public HCoord(float x, float y, float direction) {
			HCoord v1 = new HCoord(x, y);
			HCoord v2 = new HCoord(	x + (float)Math.Cos(direction / 180 * Math.PI),
									y + (float)Math.Sin(direction / 180 * Math.PI));
			HCoord g = HCoord.Cross(v1, v2);
			this.x = g.x;
			this.y = g.y;
		}

		/// <summary>
		/// Translates this point using the given vector.
		/// </summary>
		/// <param name="dx">Translation in x direction.</param>
		/// <param name="dy">Translation in y direction.</param>
		public void Translate(float dx, float dy) {
			x += dx;
			y += dy;
		}

		/// <summary>
		/// Calculates the distance of this point to another one.
		/// </summary>
		/// <param name="other">Another point.</param>
		/// <returns>Distance between the points.</returns>
		public float DistanceTo(HCoord other) {
			return (float)Math.Sqrt(Math.Pow(this.x - other.x, 2) + Math.Pow(this.y - other.y, 2));
		}

		/// <summary>
		/// Calculates the cross product between two vectors.
		/// Can be used to calculate a line between two points (if the vectors are points)
		/// or to calculate the intersect point between two lines (if the vectors are lines).
		/// </summary>
		/// <param name="v1">First vector, can be a point or a line.</param>
		/// <param name="v2">Second vector, should be the same type as the first one.</param>
		/// <returns>A line or the intersect point.</returns>
		public static HCoord Cross(HCoord v1, HCoord v2) {
			float z = v1.x * v2.y - v2.x * v1.y;
			return new HCoord(	(v1.y - v2.y) / z,
								(v2.x - v1.x) / z);
		}

	}
}
